/*
 * Powered By dico
 * Since 2017 - 2018
 */package com.seeyoui.kensite.bussiness.orderlang.order.service;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seeyoui.kensite.bussiness.orderlang.order.domain.Order;
import com.seeyoui.kensite.bussiness.orderlang.order.domain.OrderDTO;
import com.seeyoui.kensite.bussiness.orderlang.order.domain.OrderReportVO;
import com.seeyoui.kensite.bussiness.orderlang.order.domain.OrderStatisticsVO;
import com.seeyoui.kensite.bussiness.orderlang.order.domain.OrderVO;
import com.seeyoui.kensite.bussiness.orderlang.order.persistence.OrderMapper;
import com.seeyoui.kensite.bussiness.orderlang.purchaser.persistence.PurchaserMapper;
import com.seeyoui.kensite.bussiness.orderlang.purchaserUser.persistence.PurchaserUserMapper;
import com.seeyoui.kensite.common.base.service.BaseService;
import com.seeyoui.kensite.common.exception.CRUDException;

/**
 * Order
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Service
public class OrderService extends BaseService {
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private PurchaserUserMapper purchaserUserMapper;

	/**
	 * 根据ID查询单条数据
	 * @param id
	 * @return
	 * @throws CRUDException
	 */
	public Order findOne(String id) throws CRUDException{
		return orderMapper.findOne(id);
	}
	
	/**
	 * 查询数据集合,订单页面数据展示
	 * 多个条件查询
	 * @param orderCode
	 * @param startTime
	 * @param endTime
	 * @param customerName
	 * @param orderState
	 * @param paymentState
	 * @return
	 * @throws CRUDException
	 */
	public List<OrderVO> findAllListByCondition(String orderCode,
			String startTime, String endTime, /*String customerName,*/String orderState,String paymentState,
			int page, int rows, OrderDTO orderDTO) throws CRUDException {
		orderDTO.setOrderCode(orderCode);
		orderDTO.setStartTime(startTime);
		orderDTO.setEndTime(endTime);
		/*orderDTO.setCustomerName(customerName);*/
		orderDTO.setOrderState(orderState);
		orderDTO.setPaymentState(paymentState);
		orderDTO.setPage(page);
		orderDTO.setRows(rows);
		List<OrderVO> orderList = orderMapper.findAllListByCondition(orderDTO);
		return orderList;
	}
	
	/**
	 * 查询订单报表
	 * @param map
	 * @return
	 * @throws Exception 
	 */
	public List<OrderReportVO> findOrderReportByCondition(String customerType,String startTime,String endTime) throws Exception{
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("customerType", customerType);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		List<OrderReportVO> orderReportList =  orderMapper.findOrderReportByCondition(map);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(orderReportList.size() > 0){
			for (OrderReportVO orderReportVO : orderReportList) {
				orderReportVO.setOrderTimeShow(sdf.format(orderReportVO.getOrderTime()));
			}
		}
		return orderReportList;
	}
	
	/**
	 * 查询订单统计信息
	 * @param map
	 * @return
	 */
	public OrderStatisticsVO findOrderStatistics(String customerType,String startTime,String endTime) throws Exception {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("customerType", customerType);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		OrderStatisticsVO orderStatisticsVO = orderMapper.findOrderStatistics(map);
		OrderStatisticsVO orderCustomer = purchaserUserMapper.findOrderCustomer(map);
		if(orderCustomer != null ){
			orderStatisticsVO.setOrderCustomer(orderCustomer.getOrderCustomer());
		}else{
			orderStatisticsVO.setOrderAmount("0");
			orderStatisticsVO.setOrderCustomer("0");
		}
		return orderStatisticsVO;
	}
	
	/**
	 * 查询数据集合
	 * @param order
	 * @return
	 * @throws CRUDException
	 */
	public List<Order> findList(Order order) throws CRUDException {
		return orderMapper.findList(order);
	}
	
	/**
     * 查询订单详情头部部分
     * @param orderId
     * @return
     * @throws CRUDException
     */
	public Order findorderDetailForTitle(Map map) throws CRUDException {
        return orderMapper.findorderDetailForTitle(map);
    }
	
	/**
	 * 查询所有数据集合
	 * @param order
	 * @return
	 * @throws CRUDException
	 */
	public List<Order> findAll(Order order) throws CRUDException {
		return orderMapper.findAll(order);
	}
	
	/**
	 * 查询数据总数
	 * @param order
	 * @return
	 * @throws CRUDException
	 */
	public int findTotal(Order order) throws CRUDException {
		return orderMapper.findTotal(order);
	}
	
	public int findTotal() throws CRUDException {
		return orderMapper.findTotal();
	}
	
	/**
	 * 查询数据总数排除当前数据
	 * @param order
	 * @return
	 * @throws CRUDException
	 */
	public int findExTotal(Order order) throws CRUDException {
		return orderMapper.findExTotal(order);
	}
	
	/**
	 * 数据新增
	 * @param order
	 * @throws CRUDException
	 */
	public void save(Order order) throws CRUDException{
		order.preInsert();
		orderMapper.save(order);
	}
	
	/**
	 * 数据修改
	 * @param order
	 * @throws CRUDException
	 */
	public void update(Order order) throws CRUDException{
		order.preUpdate();
		orderMapper.update(order);			
	}
	
	/**
	 * 数据删除
	 * @param listId
	 * @throws CRUDException
	 */
	public void delete(List<String> listId) throws CRUDException {
		orderMapper.delete(listId);
	}
	
	/**
	 * 数据假删除
	 * @param order
	 * @throws CRUDException
	 */
	public void remove(Order order) throws CRUDException{
		order.preUpdate();
		orderMapper.remove(order);			
	}
	
}